import java.io.File;
import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录");
        String rootDir = scanner.next();
        File rootFile  =new File(rootDir);
        if(!rootFile.isDirectory()){
            System.out.println("输入的不是目录");
            return;
        }
        System.out.println("请输入要删除的关键字");
        String keyword = scanner.next();
        scanDir(rootFile,keyword);
    }

    private static void scanDir(File rootFile, String keyword) {
        File[] files = rootFile.listFiles();
        if(files == null){
            return;
        }
        for (File file:files) {
            System.out.println("遍历目录&文件"+file.getAbsolutePath());
            if(file.isFile()){
                dealFile(file,keyword);
            }else {
                scanDir(file,keyword);
            }
        }
    }

    private static void dealFile(File file, String keyword) {
        if(file.getName().contains(keyword)){
            System.out.println("发现文件"+file.getAbsolutePath()+",包含关键字！是否删除?(y/n)");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next();
            if(input.equalsIgnoreCase("y")){
                file.delete();
                System.out.println("文件已删除");
            }
        }
    }
}
